package com.stone.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.stone.bean.dto.RoleMenuAddDTO;
import com.stone.config.exception.BusinessException;
import com.stone.entity.SysRoleMenu;
import com.stone.mapper.SysRoleMenuMapper;
import com.stone.service.SysRoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author stone
 * @since 2022-04-02
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRoleMenu(RoleMenuAddDTO dto) {
        if (CollUtil.isEmpty(dto.getMenuList())) {
            throw new BusinessException("未选中菜单");
        }
        remove(new QueryWrapper<SysRoleMenu>().eq("role_id", dto.getRoleId()));
        List<SysRoleMenu> sysRoleMenuList = Lists.newArrayList();
        for (Long menuId : dto.getMenuList()) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setMenuId(menuId);
            roleMenu.setRoleId(dto.getRoleId());
            sysRoleMenuList.add(roleMenu);
        }
        saveBatch(sysRoleMenuList);
    }
}
